//
//  Application.cpp
//  LearnOpenGL
//
//  Created by Mr.Optimist on 2022/2/8.
//

#define GL_SILENCE_DEPRECATION

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include "Renderer.hpp"
#include "VertexBuffer.hpp"
#include "VertexBufferLayout.hpp"
#include "IndexBuffer.hpp"
#include "VertexArray.hpp"
#include "Shader.h"
#include "Texture.h"

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw_gl3.h"
#include "tests/Test.h"
#include "tests/TestClearColor.h"
#include "tests/TestTexture2D.h"

int main(void) {
    if (!glfwInit())    // Initialize the library
        return -1;

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);   // 设置成CORE模式
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);            // for MAC ONLY
//    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow *window = glfwCreateWindow(1280, 900, "LearnOpenGL", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window); /// Make the window's context current
//    glfwSwapInterval(1);

    if (glewInit() != GLEW_OK) std::cout << "error!" << std::endl;

    std::cout << glGetString(GL_VENDOR) << std::endl;
    std::cout << glGetString(GL_RENDERER) << std::endl;
    std::cout << glGetString(GL_VERSION) << std::endl;
    std::cout << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;


    Renderer renderer;

    // Setup ImGui binding
    ImGui::CreateContext();
    ImGui_ImplGlfwGL3_Init(window, true);
    ImGui::StyleColorsDark();

//    float r = 0.0f;
//    float increment = 0.05f;

    test::Test *currentTest = nullptr;
    test::TestMenu *testMenu = new test::TestMenu(currentTest);
    currentTest = testMenu;

    testMenu->RegisterTest<test::TestClearColor>("Clear Color");
    testMenu->RegisterTest<test::TestTexture2D>("2D Texture");

    // 渲染循环
    while (!glfwWindowShouldClose(window)) {
        renderer.Clear();

        ImGui_ImplGlfwGL3_NewFrame();
        if (currentTest) {
            currentTest->OnUpdate(0.0f);
            currentTest->OnRender();
            ImGui::Begin("Test");
            if (currentTest != testMenu && ImGui::Button("<-")) {
                delete currentTest;
                currentTest = testMenu;
            }
            currentTest->OnImGuiRender();
            ImGui::End();
        }

//        if (r > 1.0f) {
//            increment = -0.05f;
//        } else if (r < 0.0f) {
//            increment = 0.05f;
//        }
//        r += increment;


//        {
//            static float f = 0.0f;
//            static int counter = 0;
//            ImGui::Text("Hello, world!");                           // Display some text (you can use a format string too)
//            ImGui::SliderFloat3("TranslationA", &translationA.x, 0.0f, 500.0f); // 传入x的指针，xy会被分别设定好？
//            ImGui::SliderFloat3("TranslationB", &translationB.x, 0.0f, 500.0f); // 传入x的指针，xy会被分别设定好？
//            ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
//
//            ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our windows open/close state
//            ImGui::Checkbox("Another Window", &show_another_window);
//
//            if (ImGui::Button("Button"))                            // Buttons return true when clicked (NB: most widgets return true when edited/activated)
//                counter++;
//            ImGui::SameLine();
//            ImGui::Text("counter = %d", counter);
//
//            ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate,
//                        ImGui::GetIO().Framerate);
//        }

        ImGui::Render();
        ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData());
        glfwSwapBuffers(window);    // Swap front and back buffers
        glfwPollEvents();           // Poll for and process events
    }
    delete currentTest;
    if(currentTest != testMenu)
        delete testMenu;


    ImGui_ImplGlfwGL3_Shutdown();
    ImGui::DestroyContext();
    glfwTerminate();
    return 0;
}


// sudo DevToolsSecurity -enable
